package tq.com.tqteacher.mine.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.GradientDrawable;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.security.PublicKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tq.com.tqcom.util.TQSystemDefine;
import tq.com.tqcom.vh.ComViewHolder;
import tq.com.tqteacher.R;
import tq.com.tqteacher.mine.fragment.TQClassIntrFragment;
import tq.com.tqteacher.model.TQPublishType;

public class TQClassIntrAdapter extends RecyclerView.Adapter {
    private Context mContext;
//    private ArrayList<Integer> mSubjects ;
    private int subjectSelect;
//
//    private ArrayList<Integer> mCatalogs ;
    private int catSelect;
    private ArrayList<TQClassIntrFragment.SubjectBrif> mSubjectBrifs = new ArrayList<>();


    public TQClassIntrAdapter(Context pContext){
        mContext = pContext;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup pViewGroup, int pI) {
        if(pI == 1){
            View lView = LayoutInflater.from(mContext).inflate(R.layout.vh_classinr_course_layout, pViewGroup, false);
            return new CourseViewHold(lView);
        }else if(pI == 2){
            View lView = LayoutInflater.from(mContext).inflate(R.layout.vh_ptpye_layout, pViewGroup, false);
            return new ContentTypeViewHold(lView);
        }else if(pI == 3){
            View lView= LayoutInflater.from(mContext).inflate(R.layout.vh_classintr_count_layout, pViewGroup, false);
            return new CountViewHold(lView);
        }else if(pI == 4){
            View lView= LayoutInflater.from(mContext).inflate(R.layout.vh_classintr_info_layout, pViewGroup, false);
            return new CountViewHold(lView);
        }else if(pI == 5){
            View lView= LayoutInflater.from(mContext).inflate(R.layout.vh_classintr_2_layout, pViewGroup, false);
            return new ComViewHolder(lView);
        }

        View lView = LayoutInflater.from(mContext).inflate(R.layout.vh_classintr_commit_layout, pViewGroup, false);
        return new CommitViewHold(lView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder pViewHolder, int pI) {
        if(pViewHolder instanceof CourseViewHold){
            ((CourseViewHold)pViewHolder).updateSubjects();
        }else if(pViewHolder instanceof ContentTypeViewHold){
            ((ContentTypeViewHold)pViewHolder).bindData();
        }else if(pViewHolder instanceof CountViewHold){
            ((CountViewHold)pViewHolder).bindData(getTypeBrif(), pI - 2);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(position == 0) return 1;
        if(position == 1) return 2;
        if(position == 2) return 3;
        if(position == 3) return 4;
        if(position == 4) return 5;
        return 0;
    }

    @Override
    public int getItemCount() {
        return 4;
    }



    public void updateInfo(ArrayList<TQClassIntrFragment.SubjectBrif> pSubjectBrifs){
        mSubjectBrifs.clear();
        mSubjectBrifs.addAll(pSubjectBrifs);
        notifyDataSetChanged();
    }

    public ArrayList<Integer> getAllSubject(){
        if(mSubjectBrifs == null) return null;

        ArrayList<Integer> lIntegers = new ArrayList<>();
        for (TQClassIntrFragment.SubjectBrif b: mSubjectBrifs
             ) {
            lIntegers.add(b.subject);
        }

        return lIntegers;
    }
    public ArrayList<Integer> getAllCate(int su){
        TQClassIntrFragment.SubjectBrif lSubjectBrif = null;
        for (TQClassIntrFragment.SubjectBrif lBrif: mSubjectBrifs
             ) {
            if(lBrif.subject == su){
                lSubjectBrif = lBrif;
                break;
            }
        }

        ArrayList<Integer> lIntegers = new ArrayList<>();
        if(lSubjectBrif == null) return null;

        for (TQClassIntrFragment.TypeBrif brif: lSubjectBrif.mTypeBrifs
             ) {
            lIntegers.add(brif.type);
        }
        return lIntegers;
    }

    public TQClassIntrFragment.TypeBrif getTypeBrif(){
        TQClassIntrFragment.SubjectBrif lSubjectBrif = null;
        for (TQClassIntrFragment.SubjectBrif lBrif: mSubjectBrifs
                ) {
            if(lBrif.subject == subjectSelect){
                lSubjectBrif = lBrif;
                break;
            }
        }

        if(lSubjectBrif == null) return null;
        for (TQClassIntrFragment.TypeBrif brif: lSubjectBrif.mTypeBrifs
                ) {
            if(brif.type == catSelect){
                return brif;
            }
        }

        return null;
    }

    public void setSubjectSelect(int pSubjectSelect){
        subjectSelect = pSubjectSelect;
        notifyItemRangeChanged(1, 3);
    }

    public void setCatSelect(int pCatSelect){
        catSelect = pCatSelect;
        notifyItemRangeChanged(2, 2);
    }

    class CourseViewHold extends RecyclerView.ViewHolder{
        private GridView course;
        private CourseAdapter mCourseAdapter;
        private ArrayList<Integer> subjects;
        public CourseViewHold(View pView){
            super(pView);

            course = pView.findViewById(R.id.course);

            mCourseAdapter = new CourseAdapter();
            course.setAdapter(mCourseAdapter);
            course.setStretchMode(GridView.NO_STRETCH);
            course.setSelector(new ColorDrawable(Color.TRANSPARENT));

            course.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    mCourseAdapter.updateSelect(view, position);
                    setSubjectSelect(subjects.get(mCourseAdapter.select));
                }
            });
        }

        public void updateSubjects(){
            subjects = getAllSubject();
            if(subjects == null) return;

            ArrayList<String> data = new ArrayList<>();
//
            for (Integer i:
                    subjects) {
                data.add(TQSystemDefine.subjectTitle(i));
            }

            float d = mContext.getResources().getDisplayMetrics().density;
            float w = d * 80 * data.size() + d * 25 * (data.size() - 1);

            LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams((int) (w + 0.5), (int) (d * 80 + 0.5));
            course.setLayoutParams(lParams);
            course.setNumColumns(data.size());
            course.setColumnWidth((int) (d * 80 + 0.5));
            course.setHorizontalSpacing((int)(d * 25 + 0.5));

            mCourseAdapter.updateData(data);
            if(subjects.size() >= mCourseAdapter.select + 1){
                subjectSelect = subjects.get(mCourseAdapter.select);
            }

        }
    }



    class CourseAdapter extends BaseAdapter {
        private ArrayList<String> datas;
        private int select = 0;
        public CourseAdapter(){
        }

        public void updateData(ArrayList<String> pDatas){
            datas = pDatas;
            select = 0;
            notifyDataSetChanged();
        }
        @Override
        public int getCount() {
            return datas !=null ?  datas.size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return datas.get(position);
        }

        public void updateSelect(View pView, int s){
            int last = select;
            select = s;
            notifyDataSetChanged();
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View lView = convertView;
            if(lView == null){
                lView = LayoutInflater.from(mContext).inflate(R.layout.item_course_layout, parent, false);
            }

            setView(lView, position);

            return lView;
        }

        private void setView(View pView, int position){
            TextView lTextView = pView.findViewById(R.id.text);
            String txt = datas.get(position);
            lTextView.setText(txt);

            GradientDrawable lDrawable = new GradientDrawable();
            lDrawable.setShape(GradientDrawable.OVAL);

            if(position == select){
                lTextView.setTextColor(Color.WHITE);
                lDrawable.setColor(Color.parseColor("#fb4723"));
            }else{
                lTextView.setTextColor(Color.BLACK);
                lDrawable.setColor(Color.parseColor("#d8dbde"));
            }
            lTextView.setBackground(lDrawable);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
    }

    class ContentTypeViewHold extends RecyclerView.ViewHolder{
        private ContentTypeAdapter mContentTypeAdapter = new ContentTypeAdapter();
        private ArrayList<String> data = new ArrayList<>();
        private GridView mGridView ;
        private ArrayList<Integer> mCatalogs;

        public ContentTypeViewHold(View pView){
            super(pView);

            mGridView = pView.findViewById(R.id.gridView);
            mGridView.setAdapter(mContentTypeAdapter);
            mGridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
            mContentTypeAdapter.updateData();

            mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    mContentTypeAdapter.updateSelect(position);
                    setCatSelect(mCatalogs.get(mContentTypeAdapter.select));
                }
            });
        }

        public void bindData(){
            mCatalogs = getAllCate(subjectSelect);
            if(mCatalogs == null) return;

            data.clear();
            for (Integer i: mCatalogs
                 ) {
                data.add(TQPublishType.title(i));
            }
            mContentTypeAdapter.updateData();
            catSelect = mCatalogs.get(mContentTypeAdapter.select);
        }

        class ContentTypeAdapter extends BaseAdapter{
            private int select = 0;

            public void updateData(){
                float d = mContext.getResources().getDisplayMetrics().density;
                float h = d * 50 + 0.5f;
                float w = d * (124 * data.size()) + d * 25 * (data.size() - 1) + 0.5f;

                LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams((int)w, (int) h);
                mGridView.setLayoutParams(lParams);
                mGridView.setColumnWidth((int)(d * 124 + 0.5f));
                mGridView.setNumColumns(data.size());
                mGridView.setHorizontalSpacing((int)(d * 25 + 0.5f));

                select = 0;
                notifyDataSetChanged();
            }

            public void updateSelect(int s){
                select = s;
                notifyDataSetChanged();
            }
            @Override
            public int getCount() {
                return data.size();
            }


            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public Object getItem(int position) {
                return data.get(position);
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View lView = convertView;
                if(lView == null){
                    lView = LayoutInflater.from(mContext).inflate(R.layout.item_publish_type_layout, parent, false);
                }

                String t = data.get(position);
                TextView lTextView = lView.findViewById(R.id.title);
                lTextView.setText(t);

                GradientDrawable lGradientDrawable = new GradientDrawable();
                lGradientDrawable.setStroke(1, Color.parseColor("#AAAAAA"));
                lGradientDrawable.setCornerRadius(10 * mContext.getResources().getDisplayMetrics().densityDpi);
                if(select == position){
                    lGradientDrawable.setColor(Color.parseColor("#ffde02"));
                }else{
                    lGradientDrawable.setColor(Color.WHITE);
                }
                lTextView.setBackground(lGradientDrawable);
                return lView;
            }
        }
    }

    class CountViewHold extends RecyclerView.ViewHolder{
        private TextView mTextView;
        public CountViewHold(View pView){
            super(pView);
            mTextView = pView.findViewById(R.id.content);
        }

        public void bindData(TQClassIntrFragment.TypeBrif pTypeBrif, int t){
            if(pTypeBrif == null) return;

            if(t > 0){
                mTextView.setText(pTypeBrif.desc);
            }else {
                mTextView.setText(String.format("%d次", pTypeBrif.amount));
            }
        }
    }

    class CommitViewHold extends RecyclerView.ViewHolder{
        public CommitViewHold(View pView){
            super(pView);
        }

        public void bindData(TQClassIntrFragment.TypeBrif pTypeBrif){

        }
    }

    public interface IClickListener{
        void subjectChange(int subject);
        void catChange(int cat);
    }

    private IClickListener mClickListener ;
    public void setClickListener(IClickListener pListener){
        mClickListener = pListener;
    }
}
